﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ConnectToDatabase;
using ConnectToDatabase.Model;

namespace QLGiayDep.Forms
{
    /// <summary>
    /// Interaction logic for WinThemDauSP.xaml
    /// </summary>
    public partial class WinThemDauSP : Window
    {
        //private SetIDObjectClient setIdObjectClient;
        private SetID setID;
        private InsertDB insertDB;
        private SelectDB selectDB;
        private DauSanPham m_DauSanPham = new DauSanPham();
        string URL;
        
        public WinThemDauSP(string _URL)
        {
            InitializeComponent();
            URL = _URL;
            setID = (SetID)Activator.GetObject(typeof(SetID), URL + "SETID_OBJECT");
            txtMaDauSP.Text = setID.GenerateMaDauSP();
            ShowComboboxNhaPP(cmbNhaPP, 1);
            ShowComboboxLoaiSP(cmbLoaiSP, 1);

        }
        public WinThemDauSP(string m_sMaDauSP, string _URL)
        {
            InitializeComponent();
            URL = _URL;
            selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), URL + "SELECTDB_OBJECT");
            ShowComboboxNhaPP(cmbNhaPP, 1);
            ShowComboboxLoaiSP(cmbLoaiSP, 1);
            m_DauSanPham = selectDB.SelectAllDauSanPham(m_sMaDauSP);
            txtMaDauSP.Text = m_DauSanPham.MaDauSanPham.ToString();
            txtGiaBan.Text = m_DauSanPham.GiaBan.ToString();
            txtHanBan.Text = m_DauSanPham.ThoiHanBan.ToString();
            cmbLoaiSP.SelectedItem = m_DauSanPham.MaLoaiSanPham.ToString();
            cmbNhaPP.SelectedItem = m_DauSanPham.MaNhaPP.ToString();
            txtTenDauSP.Text = m_DauSanPham.TenDauSanPham.ToString();
        }
        public WinThemDauSP(string m_sMaNhaPP, string m_sMaLoaiSP, string _URL)
        {
            InitializeComponent();
            URL = _URL;
            cmbNhaPP.Items.Add(m_sMaNhaPP);
            cmbLoaiSP.Items.Add(m_sMaLoaiSP);
            setID = (SetID)Activator.GetObject(typeof(SetID), URL + "SETID_OBJECT");
            txtMaDauSP.Text = setID.GenerateMaDauSP();
        }

        void ShowComboboxNhaPP(ComboBox cmb, int TinhTrang)
        {
            selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), URL + "SELECTDB_OBJECT");
            for (int i = 0; i < selectDB.SelectAllNhaPhanPhoi(TinhTrang).Count; i++)
            {
                cmb.Items.Add(selectDB.SelectAllNhaPhanPhoi(TinhTrang)[i].MaNhaPP.ToString());
            }
        }

        void ShowComboboxLoaiSP(ComboBox cmb, int TinhTrang)
        {
            selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), URL + "SELECTDB_OBJECT");
            for (int i = 0; i < selectDB.SelectAllLoaiSanPham(TinhTrang).Count; i++)
            {
                cmb.Items.Add(selectDB.SelectAllLoaiSanPham(TinhTrang)[i].MaLoaiSanPham.ToString());
            }
        }

        private void btnThemDauSP_Click(object sender, RoutedEventArgs e)
        {
            int kt;
            insertDB = (InsertDB)Activator.GetObject(typeof(InsertDB), URL + "INSERTDB_OBJECT");
            kt = insertDB.InsertDauSanPham(txtMaDauSP.Text, cmbLoaiSP.SelectedItem.ToString(), txtTenDauSP.Text, cmbNhaPP.SelectedItem.ToString(),
                                           Int32.Parse(txtGiaBan.Text), Int32.Parse(txtHanBan.Text), 1);
            if (kt == -1)
                MessageBox.Show("Thêm sản phẩm thất bại", "Lỗi");
            else
            {
                MessageBox.Show("Thêm sản phẩm Thành Công", "Thông Báo");
                txtMaDauSP.Text = setID.GenerateMaNhaPP();
                txtTenDauSP.Clear();
                txtGiaBan.Clear();
                txtHanBan.Clear();
            }
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
